package cn.edu.zafu.bigdata.liuwenyi;

import java.io.*;
import java.util.Scanner;

public class TextFileHandler {

    // 写入文件的方法
    public static void writeToFile(String fileName, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            writer.write(content);
            System.out.println("内容已写入文件: " + fileName);
        } catch (IOException e) {
            System.out.println("写入文件时出错: " + e.getMessage());
        }
    }

    // 读取文件的方法
    public static void readFromFile(String fileName) {
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            System.out.println("文件内容:");
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println("读取文件时出错: " + e.getMessage());
        }
    }

    // 主方法，用于测试
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要保存的文本:");
        String content = scanner.nextLine();

        // 定义文件路径
        String fileName = "files/202211080227.txt";

        // 写入和读取文件
        writeToFile(fileName, content);
        readFromFile(fileName);

        scanner.close();
    }
}
